package com.lty.netty3heart;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;

/*public class HelloHandler extends SimpleChannelHandler {

	@Override
	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
		System.out.println(e.getMessage());
	}

	//调用该方法，自定义处理通道空闲业务逻辑
	@Override
	public void handleUpstream(final ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
		if (e instanceof IdleStateEvent) {
			if(((IdleStateEvent)e).getState() == IdleState.ALL_IDLE){
				System.out.println("踢玩家下线");
				//关闭会话,踢玩家下线(先给用户一个超时说明)
				ChannelFuture write = ctx.getChannel().write("time out, you will be closed!");
				//监听器作用：给客户端回送数据完成后关闭会话(Channel)
				write.addListener(new ChannelFutureListener() {
					
					public void operationComplete(ChannelFuture future) throws Exception {
						 ctx.getChannel().close();
					}
				});
			}
		} else {
			super.handleUpstream(ctx, e);
		}
	}

}*/

public class HelloHandler extends IdleStateAwareChannelHandler {

	 
	/* <p>Title: channelIdle</p> 
	* <p>Description: 通道空闲业务处理方法</p> 
	* @param ctx
	* @param e
	* @throws Exception 
	* @see org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler#channelIdle(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.handler.timeout.IdleStateEvent) 
	*/
	@Override
	public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
		DateFormat format = new SimpleDateFormat("mm:ss");
		System.out.println(format.format(new Date()) + "  " + e.getState());
		super.channelIdle(ctx, e);
	}
	
}